package filesystem;
import java.util.Date;
import java.util.Iterator;


public class CommandLine 
{
	FileSystem fileSystem = null;
	
	public static String PWD = "pwd";
	public static String CD = "cd";
	public static String ROOT = "root";
	public static String LS = "ls";
	public static String DIR = "dir";
	public static String MKDIR = "mkdir";
	
	
	public CommandLine()
	{
		Directory root = new Directory("root", "Mine too", null, new Date());
		Directory windows = new Directory("Windows", "Mine", root, new Date());
		Directory myDocument = new Directory("MyDocument", "Mine", root, new Date());
		Directory myPictures = new Directory("MyPictures", "Mine", myDocument, new Date());
		
		File a = new File("a", "Mine", windows, new Date(), 5); 
		File b = new File("b", "me", windows, new Date(), 17);
		File c = new File("c", "Mine too", windows, new Date(), 3); 
		File d = new File("d", "Mine", myDocument, new Date(), 16);
		File e = new File("e", "me", myPictures, new Date(), 200); 
		File f = new File("f", "Another me", myPictures, new Date(), 320);
		
		Link x = new Link(d, "x", "Mine", myDocument, new Date());
		Link y = new Link(windows, "y", "Mine", myPictures, new Date());
		
		
		fileSystem =  FileSystem.getInstance(root);
		//fileSystem.showAllElements();
	}
	
	public void PWDCommand()
	{		
		System.out.println(fileSystem.getCurrent().getInfo(""));
	}
	
	public void DIRCommand()
	{
		System.out.println(fileSystem.getCurrent().getDetailListing(""));	
	}
	
	public void CDCommand(String target)
	{
		Directory newCurrent = null;
				
		if ("..".equals(target))		
			newCurrent = fileSystem.getCurrent().getParent();
		else		
			newCurrent = FindDirectory(fileSystem.getCurrent(), target);		
		if (newCurrent != null)
		{
			fileSystem.setCurrent(newCurrent);
			System.out.println("New current: " + fileSystem.getCurrent().getInfo(""));
		}
		else		
			System.out.println("path specified does not exist");		
	}
	
	public void RootCommand()
	{
		fileSystem.setCurrent(fileSystem.getRoot());		
		System.out.println("Current set to root");
	}
	
	public void LSCommand()
	{
		System.out.println(fileSystem.getCurrent().getListing(""));
	}
	
	public void DIRPATHCommand(String path)
	{
		if ("..".equals(path))
		{
			if (fileSystem.getCurrent() != fileSystem.getRoot())
			{				
				System.out.println(fileSystem.getCurrent().getParent().getInfo(""));
			}
			else
				System.out.println("Command cannot be executed on root");
			return;
		}
		
		String[] pathParts = path.split("/");
		Directory currentDirectory = fileSystem.getCurrent();
		for (int i =0; i < pathParts.length - 1; i++)
		{
			currentDirectory = FindDirectory(currentDirectory, pathParts[i]);
			if (currentDirectory == null)
			{
				System.out.println("Incorrect Path!");
				return;			
			}			
		}
		
		FSElement finalFSE = FindFSElement(currentDirectory, pathParts[ pathParts.length-1 ]);
		if (finalFSE!=null)
			System.out.println(finalFSE.getInfo(""));
		else
			System.out.println("Incorrect Path!");		
	}
	
	private Directory FindDirectory(Directory dir, String target)
	{
		FSElement result;
		Iterator<FSElement> it = dir.getChildren().iterator();		
		while(it.hasNext())
		{
			result = it.next();
			if (result instanceof Directory && result.getName().equals(target.trim()))
			{
				return (Directory)result;
			}
		}
		
		return null;
	}
	
	private FSElement FindFSElement(Directory dir, String target)
	{
		FSElement result;
		Iterator<FSElement> it = dir.getChildren().iterator();		
		while(it.hasNext())
		{
			result = it.next();
			if (result.getName().equals(target.trim()))
			{
				return result;
			}
		}		
		return null;
	}

	public void MKDIRCommand(String dirName)
	{
		if (FindDirectory(fileSystem.getCurrent(), dirName) == null)
		{
			new Directory(dirName, "OnTheFly", fileSystem.getCurrent(), new Date());
			System.out.println("Directory " +dirName + " has been created");
		}
		else
			System.out.println("Directory " +dirName + " already exists!");
	}
}
